package oj;

import java.util.Stack;

public class LC227 {
    public int calculate(String s) {
        char sign='+';
        int sum=0;
        char[] ch=s.toCharArray();
        Stack<Integer> d=new Stack<>();
        for(int i=0;i<ch.length;i++){
            char c=ch[i];
            if(Character.isDigit(c)){
                sum=sum*10+(c-'0');
            }
            if(!Character.isDigit(c)  || i==ch.length-1){
                switch (sign){
                    case('+'):
                        d.push(sum);
                        break;
                    case('-'):
                        d.push(-sum);
                        break;
                    case('*'):
                        d.push(d.pop()*sum);
                        break;
                    case('/'):
                        d.push(d.pop()/sum);
                        break;
                }
                sign=c;
                sum=0;
            }
        }
        int res=0;
        while(!d.isEmpty()){
            res+=d.pop();
        }
        return  res;
    }
}

